package com.ecoolex.hotel.saas.platform.graphql;

import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.Department;
import com.ecoolex.hotel.saas.entity.SalesUser;
import com.ecoolex.hotel.saas.entity.SalesUserWallet;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.EditSalesUserDTO;
import com.ecoolex.hotel.saas.pojo.QuerySalesUserDTO;
import com.ecoolex.hotel.saas.ws.SalesUserService;
import com.ecoolex.hotel.saas.ws.StoreService;

@GraphQL2Module(type = Type.Platform, description = "商户用户")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
        key = SalesUserGraphql.AUTH,
        name = "商户用户管理",
        remark = "商户用户管理")
public class SalesUserGraphql {

    public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-SALES-USER";

    private static final String AUTH_EDIT = AUTH + "-EDIT";

    private static final String AUTH_GET = AUTH + "-GET";

    private static final String AUTH_QUERY = AUTH + "-QUERY";

    @Reference
    private SalesUserService salesUserService;

    @Reference
    private StoreService storeService;

    @Autowired
    private ConsumerMemberManager consumerMemberManager;

    @Autowired
    private StorageService storageService;

    @TypeField(type = SalesUser.class, field = "salesUserWallet", description = "钱包")
    public SalesUserWallet salesUserWallet(@Source SalesUser source) {
        return salesUserService.getSalesUserWalletByUserIdAndStoreId(source.getStoreId(), source.getUserId());
    }

    @TypeField(type = SalesUser.class, field = "consumerMember", description = "消费者用户信息")
    public ConsumerMember salesUser(@Source SalesUser user) {
        return consumerMemberManager.getByUserId(user.getUserId());
    }

    @TypeField(type = ConsumerMember.class, field = "headUrl", description = "头像Url")
    public StorageInfo headUrl(@Source ConsumerMember source) {
        return storageService.get(source.getHeadUrl());
    }

    @TypeField(type = SalesUser.class, field = "department", description = "所属部门信息")
    public Department department(@Source SalesUser source) {
        return storeService.getDepartment(source.getDepartmentId(), source.getStoreId());
    }

    @QueryField(field = "salesUser", description = "商户用户")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "商户用户详情")
    public SalesUser get(@NonNull Integer id, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return salesUserService.getSalesUser(id);
    }

    @QueryField(field = "salesUserList", description = "商户用户列表")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "商户用户列表")
    public Pagination<SalesUser> query(@NonNull QuerySalesUserDTO query, @NonNull Page page, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return salesUserService.querySalesUser(query, page);
    }

//	@MutationField(field = "addSalesUser", description = "添加商户用户")
//	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商户用户")
//	public SalesUser add(@NonNull AddSalesUserDTO add, PlatformMemberAuthority user) {
//		user.assertAuth(AUTH_ADD);
//		return salesUserService.addSalesUser(add, user.userName());
//	}

    @MutationField(field = "editSalesUser", description = "编辑商户用户")
    @PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商户用户")
    public SalesUser edit(@NonNull EditSalesUserDTO edit, PlatformMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        SalesUser old = salesUserService.getSalesUser(edit.getId());
        Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商户用户不存在");
        BeanUtil.copy(edit, old);
        return salesUserService.editSalesUser(old, user.userName());
    }

}
